--[[
	获取任务奖励
]]
local filename = "gettaskrewards.lua"

local timetool = require "timetool"
local skynet = require "skynet"
local cluster = require "skynet.cluster"
local json = require "cjson.safe"
json.encode_sparse_array(true,1,1)

local tostring = tostring

local filelog =require "filelog"
require "pubenum"

local service = nil
local CMD = {
	service = nil
}

function CMD.process(rid, msgbody)
	if not service then
		service = CMD.service
	end
	
    local taskid = msgbody.id or 0
    local taskdata = skynet.call(".tasksystem", "lua", "gettaskdata", rid)
    local taskitem = taskdata[tostring(taskid)]
    if not taskitem then
        return {retcode = EErrCode.ERR_INVALID_PARAMS, retstr = "无效的任务id"}
    end
    if taskitem.state ~= ETaskState.COMPLETED then
        return {retcode = EErrCode.ERR_INVALID_REQUEST, retstr = "任务未完成" }
    end

    --更新状态
    taskitem.state = ETaskState.HADRECEIVE
    cluster.call("datanode", ".router", "update_rk", rid, "rs_task", 0, {task_list_json = json.encode(taskdata)})

    --领取奖励
    local rewards = taskitem.rewards
    for _,item in pairs(rewards) do
        local prop_id = tonumber(item.prop_id)
        local value = tonumber(item.num)
        if value > 0 then
            if prop_id == 1 then    --房卡
                
            elseif prop_id == 2 then    --钻石
                service:charge_diamond(value, {tradeid = "xxxxxxxx"})
            elseif prop_id == 3 then    --金币
                service:change_money({coin = value},{tradeid = "xxxxxxxx"})
	        elseif prop_id then
                service:charge_prop(prop_id, value, {tradeid = "xxxxxxxx"})
            end
        end
    end   


    return {id = taskid, reward_list = json.encode(taskitem.rewards)}
end

return CMD
